package com.gxgeek.javabasic.clone.some;

import com.gxgeek.javabasic.clone.CloneUtils;
import lombok.Getter;
import lombok.Setter;

import java.io.Serializable;

/**
 * @author gaoxing
 * @create 2017-09-22 下午2:37
 */
@Getter
@Setter
public class Book implements Cloneable ,Serializable {

    //在示例1的基础上增加bookBorrow的引用
    private BookBorrow bookBorrow;
    private int id;

    private String name;


    public Book() {}

    public Book(int id, String name, BookBorrow bookBorrow) {
        this.id = id;
        this.name = name;
        this.bookBorrow = bookBorrow;
    }

    public BookBorrow getBookBorrow() {
        return bookBorrow;
    }

    public void setBookBorrow(BookBorrow bookBorrow) {
        this.bookBorrow = bookBorrow;
    }

    @Override
    public Object clone() throws CloneNotSupportedException {
        Book book = (Book)super.clone();
        //这里注释掉就是浅克隆，否则就是深克隆
//        book.bookBorrow = (BookBorrow)bookBorrow.clone();
        return book;
    }

    @Override
    public String toString() {
        return "BOOK[id="+id+",name="+name+",bookBorrow:"+bookBorrow+"]";
    }

    public static void main(String[] args) throws CloneNotSupportedException {

        BookBorrow bookBorrow = new BookBorrow(1,1);
        Book book1 = new Book(1,"基础系列1",bookBorrow);
        Book book2 = CloneUtils.clone(book1);

        System.out.println("图书1:" + book1.toString());
        System.out.println("图书2:" + book2.toString());

        book2.setName("基础系列2");
        book2.setBookBorrow(new BookBorrow(5,5));

        System.out.println("图书1:" + book1.toString());
        System.out.println("图书2:" + book2.toString());


    }
}

class BookBorrow  implements Cloneable ,Serializable{

    private int id;

    private int borstate;


    public BookBorrow(int id, int borstate) {
        this.id = id;
        this.borstate = borstate;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public int getBorstate() {
        return borstate;
    }

    public void setBorstate(int borstate) {
        this.borstate = borstate;
    }

//    @Override
//    public Object clone() throws CloneNotSupportedException {
//        return (BookBorrow)super.clone();
//    }

    @Override
    public String toString() {
        return "BookBorrow[id="+id+",borstate="+borstate+"]";
    }

}
